<?php

// +----------------------------------------------------------------------

// | Description: 客户

// +----------------------------------------------------------------------

// | Author:  Zl

// +----------------------------------------------------------------------

namespace app\crm\controller;



use app\admin\model\User;

use app\common\traits\ActionRecordTrait;

use think\facade\Db;

use app\admin\model\Common;

use app\mini\model\PersonCase as PersonCaseModel;

use think\response\Json;

use think\Validate;

use app\admin\controller\ApiCommon;



class PersonCase extends ApiCommon

{

    use ActionRecordTrait;

	/**

     * 为了数据库的整洁，同时又不影响Model和Controller的名称

     * 我们约定每个模块的数据表都加上相同的前缀，比如CRM模块用crm作为数据表前缀

     */

// 	protected $name = 'customer';

    public function initialize()

    {

        // print_r(1111);

        $action = [

            'permission' => ['casedate'],

            'allow' => ['users', 'info']

        ];

        event('CheckAuth',$action);

        $request = $this->request;

        $a       = strtolower($request->action());

        if (!in_array($a, $action['permission'])) {

            parent::initialize();

        }

    }

	/**

     * 列表

     * @param  array $request 请求参数

     * @return array

     */

    public function index(): json 

    {

        $this->param = request()->param();

        $personCaseModel = new PersonCaseModel();

        // $this->param['person_id'] = $this->customerId;// 员工端则为员工id

        // $this->param['ht'] = ;

        $setData = $this->param;

        $setData['ht'] = 1;

        $shopIds = $this->getCommonShopIdsCollectByUserType();

        $setData['shopIds'] = $shopIds;

        $data = $personCaseModel->getCaseList($setData);

        return resultArray($data);

    }

	/**

     * 列表

     * @param  array $request 请求参数

     * @return array

     */

    public function index_export(): json 

    {

        // $this->param = request()->param();

        $personCaseModel = new PersonCaseModel();

        // $this->param['person_id'] = $this->customerId;// 员工端则为员工id

        // $this->param['ht'] = ;

        $this->param['ht'] = 1;

        $this->param['export'] = 1;

        $data = $personCaseModel->getCaseList($this->param);

        $list_type = $this->param['list_type'] ?? 1;

        if ($list_type == 1) {

            $callName = [

                'sp_excellent_time' => '申请时间',

                'customer_name' =>'客户名称',

                'sex' =>'性别',

                'position_name' => '评选位置',

                'reason_name' => '疤痕位置',

                'type_name' => '疤痕类型',

                'nurse_name' => '护理师',

                'shop_name' => '门店',

                'market_name' => '归属市场',

                'selection_results' => '评选结果',

                'reviewer_name' => '审理人',

                'tg_excellent_time' => '审核时间',

            ];

        } else if ($list_type == 2) {

            $callName = [

                'sp_excellent_time' => '申请时间',

                'customer_name' =>'客户名称',

                'sex' =>'性别',

                'position_name' => '评选位置',

                'reason_name' => '疤痕原因',

                'type_name' => '疤痕类型',

                'nurse_name' => '护理师',

                'market_name' => '归属市场',

                'selection_results' => '评选结果',

                'reviewer_name' => '审理人',

                'tg_excellent_time' => '审核时间',

            ];

        } else if ($list_type == 3) {

            $callName = [

                'id' => '序号',

                'id' =>'案例编号',

                'position_name' =>'评选位置',

                'reason_name' => '疤痕原因',

                'type_name' => '疤痕类型',

                'sqr_name' => '申请人',

                'market_name' => '申请部门',

                'collection_time' => '入选时间',

            ];

        }

        // print_r($data);exit;

        $downUrl = exportExcelData('list',$callName,$data['data']['list'],'xlsx',1);

        return resultArray(['data' =>['url' => 'http://'.$_SERVER['HTTP_HOST'].$downUrl],'error' => '获取成功']);

    }

    /**

     * 添加案例

     */

     public function addCase(): json 

    {

        $this->param = request()->param();

        $personCaseModel = new PersonCaseModel();

        // // $this->param['person_id'] = $this->customerId;// 员工端则为员工id

        // // $this->param['ht'] = ;

        $setData = $this->param;

        // $setData['ht'] = 1;

        $data = $personCaseModel->addCase($setData);

        // return resultArray(['error' => $personCaseModel->getError(), 'data'=>[]]);

        return resultArray($data);

    }

    /**

     * 编辑案例

     */

     public function saveCase(): json 

    {

        $this->param = request()->param();

        $personCaseModel = new PersonCaseModel();

        // // $this->param['person_id'] = $this->customerId;// 员工端则为员工id

        // // $this->param['ht'] = ;

        $setData = $this->param;

        // $setData['ht'] = 1;

        $data = $personCaseModel->saveCase($setData);

        // return resultArray(['error' => $personCaseModel->getError(), 'data'=>[]]);

        return resultArray($data);

    }

    /**

     * 添加案例跟进

    */

     public function addCaseLog(): json 

     {

        $this->param = request()->param();

        $personCaseModel = new PersonCaseModel();

        // // $this->param['person_id'] = $this->customerId;// 员工端则为员工id

        // // $this->param['ht'] = ;

        $setData = $this->param;

        // $setData['user_id'] = $this->userInfo['id'];

        $setData['ht'] = 1;

        

        $data = $personCaseModel->addCaseLog($setData);

        if (!$data) {

            return resultArray(['error' => $personCaseModel->getError()]);

        }

        return resultArray($data);

     }

     

     /**

      * 电话案例评审

      */

      public function psCase(): json

      {

            $this->param = request()->param();

            $personCaseModel = new PersonCaseModel();

            // // $this->param['person_id'] = $this->customerId;// 员工端则为员工id

            // // $this->param['ht'] = ;

            $setData = $this->param;

            $setData['ht'] = 1;

            $this->param['user_id'] = $this->userInfo['id'];

            $data = $personCaseModel->psCase($setData);

            // return resultArray(['error' => $personCaseModel->getError(), 'data'=>[]]);

            return resultArray($data);

      }

      

      /**

      * 技术案例评审

      */

      public function jspsCase(): json

      {

            // $this->param = request()->param();

            $personCaseModel = new PersonCaseModel();

            // // $this->param['person_id'] = $this->customerId;// 员工端则为员工id

            // // $this->param['ht'] = ;

            $this->param['user_id'] = $this->userInfo['id'];

            $setData = $this->param;

            $setData['ht'] = 1;

            $data = $personCaseModel->jspsCase($setData);

            // return resultArray(['error' => $personCaseModel->getError(), 'data'=>[]]);

            return resultArray($data);

      }



      /**

      * 案例评审-加入案例库

      */

      public function addpsCase(): json

      {

            $this->param = request()->param();

            $personCaseModel = new PersonCaseModel();

            // // $this->param['person_id'] = $this->customerId;// 员工端则为员工id

            // // $this->param['ht'] = ;

            $setData = $this->param;

            $setData['ht'] = 1;

            $data = $personCaseModel->addpsCase($setData);

            // return resultArray(['error' => $personCaseModel->getError(), 'data'=>[]]);

            return resultArray($data);

      }



      /**

       * 案例详情

       */

       public function caseDate(): json

       {

           $this->param = request()->param();

            $personCaseModel = new PersonCaseModel();

            // // $this->param['person_id'] = $this->customerId;// 员工端则为员工id

            // // $this->param['ht'] = ;

            $setData = $this->param;

            $setData['ht'] = 1;

            $data = $personCaseModel->getDetail($setData);

            // return resultArray(['error' => $personCaseModel->getError(), 'data'=>[]]);

            return resultArray($data);

       }



       public function psTj(): json

       {

        //   $this->param = request()->param();

            $personCaseModel = new PersonCaseModel();

            // // $this->param['person_id'] = $this->customerId;// 员工端则为员工id

            // // $this->param['ht'] = ;

            // print_r($this->userInfo);exit;

            $this->param['user_id'] = $this->userInfo['id'];

            // print_r($this->param);exit;

            $setData = $this->param;

            $data = $personCaseModel->setTj($setData);

            if (!$data) {

                return resultArray(['error' => $personCaseModel->getError()]);

            }

            return resultArray($data);

       }

       

       /**

        * 删除案例

        */

        public function delCase(): json

       {

        //   $this->param = request()->param();

            $personCaseModel = new PersonCaseModel();

            // // $this->param['person_id'] = $this->customerId;// 员工端则为员工id

            // // $this->param['ht'] = ;

            // print_r($this->userInfo);exit;

            $this->param['user_id'] = $this->userInfo['id'];

            // print_r($this->param);exit;

            $setData = $this->param;

            $data = $personCaseModel->delCase($setData);

            if (!$data) {

                return resultArray(['error' => $personCaseModel->getError()]);

            }

            return resultArray($data);

       }

       
    public function caseHistoryLog():Json
    {
        
        $personCaseModel = new PersonCaseModel();

        $this->param = request()->param();

        $setData = $this->param;

        $data = $personCaseModel->caseHistoryLog($setData);

        // echo "<pre>";
        // print_r($this->param);
        // echo "</pre>";
        // exit;

        return resultArray($data);
        
    }

}

